<?php


namespace App\Http\Controllers\System\Dictionary;


use App\Http\Controllers\Controller;
use App\Http\Requests\System\Dictionary\Unit\UpdateRequest;
use App\Http\Requests\System\Dictionary\Unit\StoreRequest;
use App\Http\Searchs\System\Dictionary\UnitSearch;
use App\Repositories\Unit;

class UnitController extends Controller
{
    /**
     * @var Unit
     * @author luffyzhao@vip.126.com
     */
    private $unit;

    /**
     * UnitController constructor.
     * @param Unit $unit
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Unit $unit)
    {
        $this->unit = $unit;
    }

    /**
     * @param UnitSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(UnitSearch $search){
        return $this->response(
            $this->unit->paginate($search->toArray())
        );
    }

    /**
     * @param \App\Http\Requests\System\Dictionary\Currency\StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request){
        return $this->response(
            $this->unit->create($request->only(['code', 'name']))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id){
        return $this->response(
            $this->unit->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id){
        return $this->response(
            $this->unit->update($id, $request->only(['name']))
        );
    }
}